import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus";
import { useEnumStore } from "@/store/enumeration"

import useInstance from '@/hooks/useInstance';
export const productLinkConfigListStore = defineStore("productLinkConfigListStoreId", () => {
    const { global } = useInstance();
    const addAndEditRef = ref<{ show: (title: string, row?: any) => void }>()
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()

    const detailData = ref([
        {
            type: "text",
            prop: "channelKey",
            name: "渠道Key",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "productLinkConfigList",
            propsList: [

                {
                    label: '渠道名称',
                    prop: 'channelName',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '180px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })

    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {

        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })
    const getDate1 = async (type: string) => {
        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                // @ts-ignore
                listSearch.condition[it] = null
            })
        }

        try {
            const res = await equityPagePost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,

                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }
    }
    const detailBtn = async (row: any) => {
        try {
            const res = await equityPackageDetailGet({ id: row.id })
            detailRef.value?.show('edit', res.data)
        } catch (error) {
            console.log(error);
        }
    }
    const handPage = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    const delBtn = async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？`)
        if (flag) {
            const res = await equityPackageDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const AddEdit = async (type: string, row?: any) => {
        if (type == 'add') {
            addAndEditRef.value?.show(type)
        } else {
            const res = await equityPackageDetailGet({ id: row.id })
            addAndEditRef.value?.show(type, res.data)
        }
    }
    return {
        detailData, tableProp, listSearch, pageParams, addAndEditRef,
        getDate1, detailBtn, handPage, delBtn, AddEdit
    }
})